var express = require('express');
var router = express.Router();
const moment = require('moment')
const multiparty = require('multiparty')
const fs = require('fs')


/*视图路由 */
//首页

//验证网页
router.get('/baidu_verify_code-ysGrUsHMmK.html', function (req, res, next) {
    res.render('guanglian_tmpl/baidu_verify_code-ysGrUsHMmK.html', {});
});

router.get('/', function (req, res, next) {
    res.render('guanglian_tmpl/index.html', {});
});

//头部
router.get('/nav', function (req, res, next) {
    res.render('guanglian_tmpl/nav.html', {});
});

//底部
router.get('/footer', function (req, res, next) {
    res.render('guanglian_tmpl/footer.html', {});
});

//集团介绍
router.get('/introduction', function (req, res, next) {
    res.render('guanglian_tmpl/groupIntroduction/introduction.html', {});
});

//品牌文化
router.get('/brandCulture', function (req, res, next) {
    res.render('guanglian_tmpl/groupIntroduction/brandCulture.html', {});
});

//核心优势
router.get('/coreAdvantages', function (req, res, next) {
    res.render('guanglian_tmpl/groupIntroduction/coreAdvantages.html', {});
});

//荣誉与认证
router.get('/honorCertification', function (req, res, next) {
    res.render('guanglian_tmpl/groupIntroduction/honorCertification.html', {});
});


//管理层信息
router.get('/manageInfo', function (req, res, next) {
    res.render('guanglian_tmpl/groupIntroduction/manageInfo.html', {});
});

//联系
router.get('/connect', function (req, res, next) {
    res.render('guanglian_tmpl/groupIntroduction/connect.html', {});
});


//成长发展
router.get('/growth', function (req, res, next) {
    res.render('guanglian_tmpl/recruitment/growth.html', {});
})

//招聘公告
router.get('/recruitmentAd', function (req, res, next) {
    res.render('guanglian_tmpl/recruitment/recruitmentAd.html', {});
})

//校园招聘、社会招聘
router.get('/campusRecruiting', function (req, res, next) {
    res.render('guanglian_tmpl/recruitment/campusRecruiting.html', {title: req.query.title});
})

//产品
router.get('/product', function (req, res, next) {
    res.render('guanglian_tmpl/product/product.html', {title: req.query.title});
})

//产品详情
router.get('/productDetails', async (req, res, next) => {
    var id = req.query.id;
    var title = req.query.title;
    var selectSQL = 'select * from t_product where delete_flag = 0 and id=' + id + ''
    await common.prototype.operation(selectSQL).then(rest => {
        var data = rest[0];
        res.render('guanglian_tmpl/product/productDetails.html', {data: data,title:title});
    }, (error) => {
        console.log(error)
        res.send(error)
    }).catch((err) => {
        console.log(err)
    })
})

//信息公告
router.get('/announcement', function (req, res, next) {
    res.render('guanglian_tmpl/investorRelations/announcement.html', {});
})

//招聘详情
router.get('/recruitmentDetails', async (req, res, next) => {
    var id = req.query.id
    var selectSQL = 'select * from t_recruitment where delete_flag = 0 and id=' + id + ''
    await common.prototype.operation(selectSQL).then(rest => {
        var data = rest[0];
        var modifyDate = moment(rest[0].modify_date).format('YYYY-MM-DD');
        res.render('guanglian_tmpl/recruitment/recruitmentDetails.html', {data: data, date: modifyDate});
    }, (error) => {
        console.log(error)
        res.send(error)
    }).catch((err) => {
        console.log(err)
    })

})

//新闻
router.get('/news/socialResponsibility', async (req, res, next) => {
    var id = req.query.id
    var selectSQL = 'select * from t_news where delete_flag = 0 and id=' + id + ''
    await common.prototype.operation(selectSQL).then(rest => {
        var data = rest[0];
        var modifyDate = moment(rest[0].modify_date).format('YYYY-MM-DD');
        res.render('guanglian_tmpl/news/socialResponsibilityNew.html', {data: data, date: modifyDate});
    }, (error) => {
        console.log(error)
        res.send(error)
    }).catch((err) => {
        console.log(err)
    })
})

//新闻活动
router.get('/newsActivities', function (req, res, next) {
    res.render('guanglian_tmpl/newsActivities/newsActivities.html', {title: req.query.title});
})

//搜索
router.get('/search', function (req, res, next) {
    res.render('guanglian_tmpl/search.html', {});
})

//法律声明
router.get('/legalNotices', function (req, res, next) {
    res.render('guanglian_tmpl/legalNotices.html', {});
})




// //门店查询
// router.get('/storeQuery', async (req, res, next) => {
//     var SQL = 'select * from t_province where delete_flag = 0 order by orders asc';
//
//     await common.prototype.operation(SQL).then(async rest => {
//         var provinceCode = rest[0].province_code;
//         var citySQL = 'select * from t_city where delete_flag = 0 and province_code="' + provinceCode + '"';
//         await common.prototype.operation(citySQL).then(async result => {
//             res.render('guanglian_tmpl/service_center/storeQuery.html', {province: rest, city: result});
//         })
//
//     }, (error) => {
//         console.log(error)
//     }).catch((err) => {
//         console.log(err)
//     })
// });

//售后服务
router.get('/afterSales', function (req, res, next) {
    res.render('guanglian_tmpl/service_center/afterSales.html', {});
});


/*---------------------------------------------------------------------------------------------------------------------------------------------------------------- */
/*接口 */
//轮播图查询
router.post('/api/carouselImage', async (req, res, next) => {
    var selectSQL = 'select * from t_carousel_images where delete_flag = 0 order by id desc'
    await common.prototype.operation(selectSQL).then(rest => {
        res.send(rest)
    }, (error) => {
        console.log(error)
        res.send(error)
    }).catch((err) => {
        console.log(err)
    })
});

//产品列表查询
router.post('/api/product_category', async (req, res, next) => {
    var selectSQL = 'select * from t_product_category where delete_flag = 0'
    await common.prototype.operation(selectSQL).then(rest => {
        res.send(rest)
    }, (error) => {
        console.log(error)
        res.send(error)
    }).catch((err) => {
        console.log(err)
    })
});

//产品查询
router.post('/api/product', async (req, res, next) => {
    var condition = '';
    var begin = 0;
    //通过分类id查询所属的产品
    var title = req.body.title;
    // if (data.page) {
    //     begin = (data.page - 1) * 6;
    //     condition = condition + 'order by modify_date desc limit ' + begin + ',6'
    // }' + condition + '
    var categorySql = 'select * from t_product_category where delete_flag = 0 and title ="' + title + '"'
    await common.prototype.operation(categorySql).then(async rest => {
        var id = rest[0].id;
        var productSQL = 'select * from t_product where delete_flag = 0 and  parent_id="' + id + '"'
        await common.prototype.operation(productSQL).then( async result => {
            res.send({data: result, count: rest.length})
        })
    }, (error) => {
        console.log(error)
        res.send(error)
    }).catch((err) => {
        console.log(err)
    })



});

// 招聘查询
router.post('/api/campusRecruiting/list', async (req, res, next) => {
    var type = req.body.type;
    var selectSQL = 'select * from t_recruitment where delete_flag = 0 and  type="' + type + '"'
    await common.prototype.operation(selectSQL).then(rest => {
        res.send({
            data: rest
        })
    }, (error) => {
        console.log(error)
        res.send({
            type: 'error', error: error
        })
    }).catch((err) => {
        console.log(err)
    })
});

/*
 * 上传简历
 * @return
 *
 */
router.post("/upload/resume/addFile", async (req, res, next) => {
    let form = new multiparty.Form();
    form.encoding = 'utf-8';
    form.uploadDir = './public/upload/resume';
    form.parse(req, function (err, fields, files) {
        try {
            let inputFile = files.file[0];
            var resName = inputFile.originalFilename;
            let newPath = form.uploadDir + "/" + resName;
            fs.renameSync(inputFile.path, newPath);
            res.send({
                code: 0, msg: '上传成功',
                data: "http://localhost:8080/upload/resume/" + resName,
                name: resName
            });
        } catch (err) {
            console.log(err);
            res.send({
                code: 404, msg: "上传失败！"
            });
        }
        ;
    })
})


/*
 * 提交简历信息
 * @return
 *
 */
router.post('/api/apply/add', async (req, res, next) => {
    var data = req.body;
    var date = moment().format('YYYY-MM-DD HH:mm:ss');
    var SQL = 'INSERT INTO `t_apply`(name,sex,marriage,mail,political,hometown,education,school,professional,date,language,phone,resume,recruitment_id,recruitment_name,create_date,modify_date,delete_flag) VALUES("' + data.name + '","' + data.sex + '","' + data.marriage + '","' + data.mail + '","' + data.political + '","' + data.hometown + '","' + data.education + '","' + data.school + '","' + data.professional + '","' + data.date + '","' + data.language + '","' + data.phone + '","' + data.resume + '","' + data.id + '","' + data.recruitmentName + '","' + date + '","' + date + '",0) '
    await common.prototype.operation(SQL).then(rest => {
        res.send({
            type: 'success', content: '添加成功'
        })
    }, (error) => {
        console.log(error)
        res.send({
            type: 'error', content: '添加失败'
        })
    }).catch((err) => {
        console.log(err)
    })
});

//招聘搜索
router.post('/api/recruitment/search', async (req, res, next) => {
    var data = req.body;
    var condition = '';
    var begin = 0;
    if (data.name) {
        condition = condition + 'and position("' + data.name + '" IN name)'
    }
    if (data.position_type) {
        condition = condition + 'and position("' + data.position_type + '" IN position_type)'
    }
    if (data.city) {
        condition = condition + 'and position("' + data.city + '" IN city)'
    }
    if (data.name) {
        condition = condition + 'and position("' + data.name + '" IN name)'
    }
    if (data.page) {
        begin = (data.page - 1) * 10;
        condition = condition + 'order by modify_date desc limit ' + begin + ',10'
    }
    var SQL = 'select * from `t_recruitment` where delete_flag=0 and type ="' + data.type + '" ' + condition + ' ';
    await common.prototype.operation(SQL).then(rest => {
        res.send({
            type: 'success', content: '搜索成功', data: rest, count: rest.length
        })
    }, (error) => {
        console.log(error)
        res.send({
            type: 'error', content: '搜索失败'
        })
    }).catch((err) => {
        console.log(err)
    })
});

//建设动态列表
router.post('/api/constructionDynamics/list', async (req, res, next) => {
    var data = req.body;
    var selectSQL = 'select * from t_party_building where delete_flag = 0 and  type="' + data.type + '" and language="' + data.language + '"'
    await common.prototype.operation(selectSQL).then(rest => {
        res.send({
            data: rest
        })
    }, (error) => {
        console.log(error)
        res.send({
            type: 'error', error: error
        })
    }).catch((err) => {
        console.log(err)
    })
});

//社会责任分页
router.post('/api/news/list', async (req, res, next) => {
    var data = req.body;
    var condition = '';
    var begin = 0;
    if (data.title) {
        condition = condition + 'and position("' + data.title + '" IN title)'
    }
    // if (data.category_name) {
    //     condition = condition + 'and position("' + data.category_name + '" IN category_name)'
    // }
    if (data.page) {
        begin = (data.page - 1) * 6;
        condition = condition + 'order by modify_date desc limit ' + begin + ',6'
    }
    var SQL = 'select * from `t_news` where delete_flag=0 and category_name ="' + data.category_name + '" ' + condition + ' ';
    await common.prototype.operation(SQL).then(rest => {
        res.send({
            type: 'success', content: '搜索成功', data: rest, count: rest.length
        })
    }, (error) => {
        console.log(error)
        res.send({
            type: 'error', content: '搜索失败'
        })
    }).catch((err) => {
        console.log(err)
    })
});

//新闻前三个
router.post('/api/news/topThreeList', async (req, res, next) => {
    var data = req.body;
    var SQL = 'select * from `t_news` where delete_flag=0 and category_name ="' + data.category_name + '" order by modify_date desc limit 1,3';
    await common.prototype.operation(SQL).then(rest => {
        res.send({
            type: 'success', content: '查询成功', data: rest, count: rest.length
        })
    }, (error) => {
        console.log(error)
        res.send({
            type: 'error', content: '查询失败'
        })
    }).catch((err) => {
        console.log(err)
    })
});

//公告列表分页
router.post('/api/news/announcementList', async (req, res, next) => {
    var data = req.body;
    var condition = '';
    var categoryName = "公告";
    var begin = 0;
    if (data.page) {
        begin = (data.page - 1) * 6;
        condition = condition + 'order by hotspot_flag desc limit ' + begin + ',6'
    }

    var SQL = 'select * from `t_news` where delete_flag=0 and category_name ="' + categoryName + '" ' + condition + ' ';
    await common.prototype.operation(SQL).then(rest => {
        res.send({
            type: 'success', content: '搜索成功', data: rest, count: rest.length
        })
    }, (error) => {
        console.log(error)
        res.send({
            type: 'error', content: '搜索失败'
        })
    }).catch((err) => {
        console.log(err)
    })
});


//搜索列表
router.post('/api/search/list', async (req, res, next) => {
    var data = req.body;
    var productSql = 'select * from `t_product` where delete_flag=0 and title like "%' + data.title + '%"';

    var newsSql = 'select * from `t_news` where delete_flag=0 and parent!=2 and title like "%' + data.title + '%"';
    var announcementSql = 'select * from `t_news` where delete_flag=0 and parent=2 and title like "%' + data.title + '%"';
    if (data.title) {
        await common.prototype.operation(newsSql).then(async newsRest => {
            await common.prototype.operation(productSql).then(async productRest => {
                await common.prototype.operation(announcementSql).then(async announcementRest => {
                    res.send({
                        type: 'success', content: '搜索成功', newsData: newsRest, newsCount: newsRest.length,
                        productData: productRest, productCount: productRest.length, announcementData: announcementRest,
                        announcementCount: announcementRest.length
                    })
                })

            })
        })
    } else {
        res.send({
            type: 'success', content: '未找到结果'
        })
    }

});


/*
 * 通过省cdoe查询市
 * @return
 *
 */
router.post('/storeQuery/city', async (req, res, next) => {
    var provinceCode = req.body.provinceCode;
    var SQL = 'select * from t_city where delete_flag = 0 and province_code="' + provinceCode + '"';
    await common.prototype.operation(SQL).then(rest => {
        res.send({
            code: 0, msg: '操作成功',
            data: rest
        })
    }, (error) => {
        console.log(error)
        res.send({
            code: 404, msg: '操作失败',
            data: error
        })
    }).catch((err) => {
        console.log(err)
    })
});

/*
 * 查询所有门店地址
 * @return
 *
 */
router.post('/storeQuery/storeAddress', async (req, res, next) => {
    var SQL;
    var cityName = req.body.cityName;
    if (cityName) {
        SQL = 'select * from t_store_address where delete_flag = 0 and address like "%' + cityName + '%"'
    } else {
        SQL = 'select * from t_store_address where delete_flag = 0'
    }
    await common.prototype.operation(SQL).then(rest => {
        res.send({
            code: 0, msg: '操作成功',
            data: rest
        })
    }, (error) => {
        console.log(error)
        res.send({
            code: 404, msg: '操作失败',
            data: error
        })
    }).catch((err) => {
        console.log(err)
    })
});


module.exports = router;